package Threading;

import java.util.Random;

class SumOperator{
    long SumEven = 0;
    long SumOdd = 0;

    public void addEvenSum(int num){
        SumEven += num;
    }
    public void addOddSum(int num){
        SumOdd += num;
    }
    public void result(){
        System.out.println("奇数之和为"+SumOdd);
        System.out.println("偶数之和为"+SumEven);
        System.out.println("总和为"+SumEven+SumOdd);
    }
}

public class demo4 {
    public static void main(String[] args) throws InterruptedException {
        long starttime = System.currentTimeMillis();
        int total = 1000_0000;
        int[] arr = new int[total];
        Random random = new Random();
        for (int i = 0; i < total; i++) {
            int num = random.nextInt(100) + 1;
            arr[i] = num;
        }

        SumOperator operator = new SumOperator();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < total; i += 2) {
                operator.addEvenSum(arr[i]);
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 1; i < total; i += 2) {
                operator.addOddSum(arr[i]);
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();

        long endtime = System.currentTimeMillis();

        operator.result();
        System.out.println("总用时" + (endtime - starttime));

    }
}
